<?php

class baoyangAction extends HomeAction {
    public function index() {
        $mapAuto['parentId']=0;
        $auto=M("auto")->where($mapAuto)->select();
        $this->assign("auto",$auto);
        $this->display();
    }
 
    public function myOrder(){
        if(!empty($_SESSION['user'])){
            $map['hx_order.user_id']=$_SESSION['user']['user_id'];
            $Model = M('order');
            $order = $Model->join('hx_user_auto ua on hx_order.ua_id=ua.ua_id')->join('hx_auto_model m on hx_order.model_id=m.model_id')->field('hx_order.*,ua.vin,ua.number,m.*')->where($map)->select();
            $this->assign("order",$order);
        }
        $this->display();
    }

    //验证短信码
    public function getOrderData(){
        if(empty($_SESSION['searchOrder']['phone'])){
            $this->ajaxReturn(null, "失败", 0);
        }

        $S = M("sms");
        $mapSms['desc'] = $_POST['code'];
        $mapSms['to_phone'] = $_SESSION['searchOrder']['phone'];
        $mapSms['isused'] = 0;

        $code = $S->field("create_time")->where($mapSms)->order("create_time desc")->limit(1)->select();

        if ($code == false) {
            $this->ajaxReturn(null, "验证码错误", -6);
        }
        $code = $code[0]['create_time'];
        //$S->where($mapSms)->setField("isused",1);

        if (($code + 5 * 60) < time()) {
            $this->ajaxReturn(null, "验证码已过期，请重新获取", -8);
        }

        $map['hx_order.username']=$_SESSION['searchOrder']['phone'];
        $field="order_id,order_num,user_id,yy_time,time,total_price,hx_order.status,name,address,brand_name,model_name,model_pl,model_year,brand_logo";
        $PO=M("order");
        $order=$PO->field($field)->where($map)->join("hx_company ON hx_company.id=hx_order.wx_id")->join("hx_auto_model ON hx_auto_model.model_id=hx_order.model_id")->select();
        if($order){
            $this->ajaxReturn($order, "成功", 1);
        }else{
            $this->ajaxReturn(null, "获取失败，请重试！", 0);
        }
    }

    //获取短信验证码-查询订单
    public function searchOrderGetCode(){
        $S = M("sms");
        $randStr = str_shuffle('1234567890');
        $rand = substr($randStr, 0, 6);
        
        $sms['to_phone'] = $_POST['phone'];
        $sms['content'] = "您的验证码是：" . $rand . "。请在5分钟内使用，过期无效！";
        $_SESSION['searchOrder']['phone']= $_POST['phone'];

        import("@.ORG.MobileSms");
        $mobilesms = new MobileSms();
        $rs = $mobilesms->send($sms['to_phone'], $sms['content']);

        if ($rs !== false) {
            $sms['type'] = 3;
            $sms['create_time'] = time();
            $sms['isused'] = 0;
            $sms['desc'] = $rand;
            $sms["out_id"] = $rs;
            $res = $S->add($sms);
            if ($res) {
                $this->ajaxReturn($rand, "验证码发送成功", 1);
            } else {
                $this->ajaxReturn(null, "发送验证码失败，请重试", -7);
            }
        }else{
            $this->ajaxReturn(null, "验证码发送失败，请重试！", 0);
        }
    }

    //验证码方式登录
    private function loginCode(){
        $U=M("user");
        if(!empty($_SESSION['open_id'])){
            $U->where("username=".$_POST['phone'])->setField("open_id",$_SESSION['open_id']);
        }
        $map['username']=$_POST['phone'];
        $field="user_id,username,nickname,headImage,realname";
        $_SESSION['user']=$U->field($field)->where($map)->find();
    }

    //选择服务
    public function step2(){
        $mapAuto['model_id']=$_GET['id'];
        $auto=M("auto_model")->where($mapAuto)->find();
        $this->assign("auto",$auto);

        $PJ=M("peijian");
        $jiyou=$PJ->where("type_id=1 and status=3")->order('pj_csprice desc')->select();
        $jiyou4=array();
        $jiyou1=array();

        $jiyouNine=260; //99套餐所用机油
        $_SESSION['order']['jyDef']=$jiyouNine;

        //判断使用机油等级
        $jiyouId4=260;//默认普通4升机油 260
        $jiyouId1=261;//默认普通1升机油 261
        if(in_array($auto['brand_id'],array(1,2,8,9,11,14,15,36,62,65,67,70,73,85,88,119,152))){
            $jiyouId4=254;//默认普通4升机油
            $jiyouId1=257;//默认普通1升机油
        }

        for($i=0;$i<count($jiyou);$i++){
            if($jiyou[$i]['weight']==4){
                $jiyou4[]=$jiyou[$i];
            }else if($jiyou[$i]['weight']==1){
                $jiyou1[]=$jiyou[$i];
            }
            //推荐机油
            if($jiyou[$i]["pj_id"]==$jiyouId4){
                $jiyouRec4=$jiyou[$i];  //推荐的4升机油
            }else if($jiyou[$i]["pj_id"]==$jiyouId1){
                $jiyouRec1=$jiyou[$i];  //推荐的1升机油
            }

            if($jiyou[$i]["pj_id"]==$jiyouNine){
                $jiyouDef4=$jiyou[$i];
            }else if($jiyou[$i]["pj_id"]==C("JIYOU")[$jiyouNine]){
                $jiyouDef1=$jiyou[$i];
            }
        }

        //session记录数据
        $_SESSION['order']['model_id']=$_GET['id'];

        $this->assign("jiyou4",$jiyou4);
        $this->assign("jiyou1",$jiyou1);

        $this->assign("jiyouRec4",$jiyouRec4);
        $this->assign("jiyouRec1",$jiyouRec1);

        $this->assign("jiyouDef4",$jiyouDef4);
        $this->assign("jiyouDef1",$jiyouDef1);

        //获取机滤
        $mapAuto['chenben']=array("neq",0); 
        $Z=M("zhu");
        $jilv=$Z->where($mapAuto)->select();
        if($jilv){
            for($i=0;$i<count($jilv);$i++){
                $jilvId[]=$jilv[$i]["pj_id"];
            }
            $mapJV['pj_id']=array("in",implode($jilvId,","));
            $jilv=$PJ->where($mapJV)->select();
        }else{
            $jilv[]=$PJ->where("pj_id=278")->find();
        }
        $_SESSION['order']['jvDefId']=$jilv[0]['pj_id'];
        $this->assign("jilv",$jilv);
        $this->display();
    }

    public function chooseJY(){
        $PJ=M("peijian");
        $map['pj_id']=$_POST['pj_id'];
        $map['status']=3;
        $field="pj_id,type_id,logo,pj_name,pj_xinghao,pj_type,pj_scprice,pj_csprice,pj_cbprice,content,weight";


        $data['four']=$PJ->where($map)->find();

        $mapOne['pj_id']=C("JIYOU")[$_POST['pj_id']];
        $data['one']=$PJ->where($mapOne)->find();

        if($_POST['pj_id']==286||$_POST['pj_id']==292){
            $data['four']=$data['one'];
        }

        //$data['nine']=$_POST['pj_id']==$_SESSION['order']['jyDef']?true:false;
        $this->ajaxReturn($data,"success",1);
    }

    public function step3(){
        $_SESSION['order']['goodsId']=$_GET['val'];
        import("@.ORG.Page");
        $map['wx_id']=array("in","8,20,23,39,135,145,221,271,275,277,378,424,432,433,434,435,436,437,438,439");

        $C=M("company");
        $count = $C->where($map)->count();
        $p = new Page($count, 10);
        $company = $C->where($map)->limit($p->firstRow . ',' . $p->listRows)->order('zone desc')->select();

        $page = $p->show();
        $this->assign('page', $page);
        $this->assign('company', $company);
        $this->display();
    }

    public function step4(){
        if(empty($_POST)){
            redirect(U('index'));
        }

        $_SESSION['order']['wx_id']=$_POST['wx_id'];
        $mapAuto['model_id']=$_SESSION['order']['model_id'];
        $auto=M("auto_model")->where($mapAuto)->find();

        $PJ=M("peijian");
        $map['pj_id']=array("in",$_SESSION['order']['goodsId']);
        $peijian=$PJ->where($map)->select();

        if($peijian[0]['type_id']==1){
            $jiyou4=$peijian[0];
            $jilv=$peijian[1];
        }else{
            $jiyou4=$peijian[1];
            $jilv=$peijian[0];
        }

        if($jiyou4['pj_id']==287||$jiyou4['pj_id']==293){

            $goods[0]['id']=$jiyou4['pj_id'];
            $goods[2]['id']=$jiyou4['pj_id'];
            $goods[1]['id']=$jilv['pj_id'];

            $goods[0]['name']=$jiyou4['pj_name']." 1L";
            $goods[2]['name']=$jiyou4['pj_name']." 1L";
            $goods[1]['name']=$jilv['pj_pinpai']." ".$jilv['pj_xinghao'];

            $goods[0]['count']=4;
            $goods[2]['count']=($auto['capacity'])-4;
            $goods[1]['count']=1;

            $goods[0]['price']=$jiyou4['pj_csprice'];
            $goods[2]['price']=$jiyou4['pj_csprice'];
            $goods[1]['price']=$jilv['pj_csprice'];

        }else{

            $mapOne['pj_id']=C("JIYOU")[$jiyou4['pj_id']];
            $jiyou1=$PJ->where($mapOne)->find();
            $goods[0]['id']=$jiyou4['pj_id'];
            $goods[2]['id']=$jiyou1['pj_id'];
            $goods[1]['id']=$jilv['pj_id'];

            $goods[0]['name']=$jiyou4['pj_name']." 4L";
            $goods[2]['name']=$jiyou1['pj_name']." 1L";
            $goods[1]['name']=$jilv['pj_pinpai']." ".$jilv['pj_xinghao'];

            $goods[0]['count']=floor(($auto['capacity'])/4);
            $goods[2]['count']=($auto['capacity'])%4;
            $goods[1]['count']=1;

            $goods[0]['price']=$jiyou4['pj_csprice'];
            $goods[2]['price']=$jiyou1['pj_csprice'];
            $goods[1]['price']=$jilv['pj_csprice'];
        }

        $mapCompany['id']=$_SESSION['order']['wx_id'];
        $company=M("company")->where($mapCompany)->find();
        $this->assign("company",$company);
        $this->assign("goods",$goods);
        $this->assign("auto",$auto);
        $this->display();
    }

    public function coupon(){
        // if(empty($_REQUEST['code']) || ($_SESSION['order']['jyDef'].','.$_SESSION['order']['jvDefId'])!=$_SESSION['order']['goodsId']){
        //     $this->ajaxReturn(null,'默认套餐才享此优惠哦',-1001);
        // }
        
        $map['code'] = $_REQUEST['code'];
        $data = M('coupon_user as l')->join("left join hx_coupon as c ON c.c_id=l.c_id")->field('c.c_id,l.number,l.code,l.status,c.money,c.content,c.low_money,c.high_money,c.type')->where($map)->find();
        
        if($data){
            if($data['status']==1){
                $this->ajaxReturn($data,'此优惠券可使用',1);
            }elseif($data['status']==0){
                $this->ajaxReturn($data,'此优惠券已使用',-1003);
            }
        }else{
            $this->ajaxReturn(null,'优惠码错误',-1002);
        }
    }

    public function step5(){

        $mapCompany['wx_id']=$_SESSION['order']['wx_id'];
        $company=M("company")->where($mapCompany)->find();
        $_SESSION['order']['wx_name']=$company['name'];
        $_SESSION['order']['wx_address']=$company['address'];
        $_SESSION['order']['source']=$_POST['source'];
        $_SESSION['order']['phone']=$_POST['phone'];

        $order_id=$this->saveOrder();
        if($order_id){
            $this->alipay($order_id);
        }else{
            $this->assign("txt","支付失败");
            $this->assign("status",0);
            $this->display();
        }
    }

    //根据车型获取保养数据
    public function getGoodsList(){
        $model_id=$_POST['model_id'];
        $data['jilv']=$this->getJL($model_id);

        $AM=M("auto_model_copy");
        $jyId=$AM->where("model_id=".$_POST['model_id'])->getField("baoyang");
        $data['jiyou']=$this->getJY($jyId);
        unset($data['jiyou']['buyPrice']);
        unset($data['jilv']['buyPrice']);
        $this->ajaxReturn($data, "获取成功", 1);
    }
    
    //支付宝支付
    public function alipay($order_id=null)
    {
    	vendor('alipay/alipay_submit');
    	vendor('alipay/alipay_core');
    	vendor('alipay/alipay_md5');
    	if(empty($order_id)){
    		$order_id = $_REQUEST['order_id'];
    	}
    	$Info = M('order')->where("order_id=$order_id")->find();
    	$alipay_config = $this->alipayConfig();
    	//支付类型
    	$payment_type = "1";
    	//必填，不能修改
    	//服务器异步通知页面路径
    	$notify_url = "http://wap.99qby.com/pay/alipay_notify";
    	//需http://格式的完整路径，不能加?id=123这类自定义参数
    	//页面跳转同步通知页面路径
    	$return_url = "http://wap.99qby.com/payment/alipay/return_url.php";
    	//需http://格式的完整路径，不能加?id=123这类自定义参数，不能写成http://localhost/
    	//商户订单号
    	$out_trade_no = $Info['order_num'];
    	//商户网站订单系统中唯一订单号，必填
    	//订单名称
    	$subject = $Info['order_name'];
    	//必填
    	//付款金额
    	$total_fee = $Info['all_price'];
    	if($Info['user_id']==208){
    		$total_fee = 0.01;
    	}
    	//必填
    	//订单描述
    	$body = $Info['order_name'];
    	//商品展示地址
    	$show_url = 'http://wap.99qby.com/';
    	//需以http://开头的完整路径，例如：http://www.商户网址.com/myorder.html
    	//防钓鱼时间戳
    	$anti_phishing_key = "";
    	//若要使用请调用类文件submit中的query_timestamp函数
    	//客户端的IP地址
    	$exter_invoke_ip = "";
    	//非局域网的外网IP地址，如：221.0.0.1
    	//构造要请求的参数数组，无需改动
    	$parameter = array(
    			"service" => "create_direct_pay_by_user",
    			"partner" => trim($alipay_config['partner']),
    			"seller_email" => trim($alipay_config['seller_email']),
    			"payment_type"	=> $payment_type,
    			"notify_url"	=> $notify_url,
    			"return_url"	=> $return_url,
    			"out_trade_no"	=> $out_trade_no,
    			"subject"	=> $subject,
    			"total_fee"	=> $total_fee,
    			"body"	=> $body,
    			"show_url"	=> $show_url,
    			"anti_phishing_key"	=> $anti_phishing_key,
    			"exter_invoke_ip"	=> $exter_invoke_ip,
    			"_input_charset"	=> trim(strtolower($alipay_config['input_charset']))
    	);
    	//建立请求
    	$alipaySubmit = new AlipaySubmit($alipay_config);
    	$html_text = $alipaySubmit->buildRequestForm($parameter,"get", "确认");
    	echo $html_text;
    }
    
    private function alipayConfig(){
    	$alipay_config = array();
    	$alipay_config['partner']		= '2088611901720311';
    	$alipay_config['seller_email']	= 'lq@haixiuyizhan.com';
    	$alipay_config['key']			= '5joc6c4nzazo6b099sra6dhfg1sxo7r5';
    	$alipay_config['sign_type']    = strtoupper('MD5');
    	$alipay_config['input_charset']= strtolower('utf-8');
    	$alipay_config['cacert']    = getcwd().'\\cacert.pem';
    	$alipay_config['transport']    = 'http';
    	return $alipay_config;
    }
    
}